<?php
/**
* 会员UDID记录
*/
namespace app\api\model;
use think\Model;
use think\facade\Url;
use think\Loader;
use think\Db;
use phpqrcode\QRcode;

class MemberUdid extends Base
{
    
    
    /**
    * 更新会员udid记录
    * @param mid int 会员id
    * @param udid string 设备UDID
    * @return bool
    */
    public function _updateMemberUdidRecord($mid, $udid){
        $data = $this->where([['mid', '=', $mid], ['udid', '=', $udid]])->find();
        
        $ret = $this->_checkUdidNum($udid);
        if(!$ret){
            $this->error = $this->getError();
            return false;
        }
        if(empty($data)){
            $time = time();
            $sql = "insert into sy_member_udid(mid, udid, created) select $mid, '$udid', $time from sy_member m where m.id=$mid and m.status =1 and (select IFNULL(count(mm.id), 0) as ii from sy_member_udid mm where mm.mid = $mid and mm.udid = '$udid') = 0 ";
            //echo $sql;die;
            $ret = Db::execute($sql);
            if(!$ret){
                $this->erro = "udid记录更新失败";
            }
 
        }
        return true;
    }
    
    
    /**
    * 校验该设备登陆的手机号数量
    */
    public function _checkUdidNum($udid){
        $sys = $this->getConfig();
        $num = $this->where([['udid', '=', $udid]])->count();
        
        if($num >= $sys['APPRENTICE_MAX']){
            $this->error = "设备可登陆的手机号达到上限";
            return false;
        }
        
        return true;
        
    }
    
}